#include<iostream>
using namespace std;
const int N=1e4+10;
long long arr[N];
int head, tail;
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		int oper;
		cin>>oper;
		if(oper==1)
		{
			long long x;
			cin>>x;
			arr[tail++]=x;	
		}	
		else if(oper==2)
		{
			if(head==tail) cout<<"ERR_CANNOT_POP"<<endl;
			else head++;
		}
		else if(oper==3)
		{
			if(head==tail) cout<<"ERR_CANNOT_QUERY"<<endl;
			else cout<<arr[head]<<endl;
		}
		else
			cout<<tail-head<<endl;
	} 
	return 0;
}
